﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.DomainModel.Repositories;
using Enterprise.Framework.Core.Exceptions;
using Resto.CostControl.Core.Domain.Resource;
using Enterprise.Framework.DomainModel.Specifications;
using Resto.CostControl.Core.Enums;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Domain.Model
{
    /// <summary>
    /// 原材料采购单
    /// </summary>
    public partial class MaterialPurchase: IAggregationRoot
    {
        #region 构造函数

        internal MaterialPurchase() {
            this.OrderState = MaterialPurchaseState.Draft.ToString();
            this.ID = IDHelper.CreateNewID();
        }

        #endregion

        #region 私有属性

        private IEntityRepository<MaterialPurchase> _Repository;
        private IEntityRepository<MaterialPurchase> Repository
        {
            get
            {
                if (_Repository == null)
                    _Repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<MaterialPurchase>();

                return _Repository;
            }
        }

        #endregion

        #region 业务方法

        internal void Add()
        {
            Validate();

            this.CreateAudit = new Audit();
            this.LastUpdateAudit = new Audit();

            this.Repository.Add(this);
        }

        internal void Update()
        {
            Validate();

            this.LastUpdateAudit = new Audit();

            this.Repository.Update(this);
        }

        internal void Delete()
        {
            this.Repository.Delete(this);
        }

        internal void AddItem(MaterialPurchaseItem item)
        {
            bool result = this.Items.Where(p => p.MaterialID == item.MaterialID).Count() > 0;
            if (result)
                throw new BusinessValidationException(SupplierResource.ValidateException_PurchaseItem_Repeat);

            this.Items.Add(item);
        }

        internal void RemoveItem(string itemID)
        {
            MaterialPurchaseItem item = this.Items.Where(p => p.ID == itemID).FirstOrDefault();
            if (item != null)
            {
                this.Items.Remove(item);
                item.Delete();
            }
        }

        internal MaterialPurchaseItem GetItem(string itemID)
        {
            return this.Items.Where(p => p.ID == itemID).FirstOrDefault();
        }

        //internal void ClearItems()
        //{
        //    MaterialPurchaseItem[] array = this.Items.ToArray();
        //    foreach (MaterialPurchaseItem item in array)
        //        item.Delete();

        //    this.Items.Clear();
        //}

        internal void UpdateState(MaterialPurchaseState state)
        {
            MaterialPurchaseState currentState = (MaterialPurchaseState)Enum.Parse(typeof(MaterialPurchaseState), this.OrderState);

            if (currentState == MaterialPurchaseState.Storage)
                throw new BusinessException(SupplierResource.ValidateException_PurchaseOrderState_UpdateEnd);

            if (currentState.GetHashCode() <= state.GetHashCode())
            {
                this.OrderState = state.ToString();                
            }
            else
            {
                string message = string.Format(SupplierResource.ValidateException_PurchaseOrderState_NotUpdate, this.OrderState, state.ToString());
                throw new BusinessException(message);
            }
        }

        #endregion

        #region 验证方法

        protected void Validate()
        {
            List<string> messageList = new List<string>();
                        
            ValidateCodeRepeat(messageList);

            if (messageList.Count > 0)
                throw new BusinessValidationException(messageList.ToArray());
        }

        
        private void ValidateCodeRepeat(IList<string> messageList)
        {
            ISpecification<MaterialPurchase> spec = Specification<MaterialPurchase>.Create(p => p.Code == this.Code);
            spec = spec.And(Specification<MaterialPurchase>.Create(p => p.ID != ID));

            bool result = Repository.Count(spec) > 0;
            if (result)
                messageList.Add(string.Format(SupplierResource.ValidateException_PurchaseCode_Repeat, this.Code));
        }

        #endregion
    }
}
